//
//  MerchantIntroduce.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/6/5.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "MerchantIntroduce.h"

@interface MerchantIntroduce ()<IGAvPlayerManagerDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tagViewW;

@end

@implementation MerchantIntroduce

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.tagView.tagViewWidth = self.tagViewW;
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PlayerChangePlayStateWith:) name:kIGAvPlayerManagerState object:nil];
}

- (void)setPlaypath:(NSString *)playpath {
    
    _playpath = playpath;
    
    if (playpath && [playpath isKindOfClass:[NSString class]]) {
        
        self.playBtn.hidden = NO;
    } else {
        
        self.playBtn.hidden = YES;
    }
}

- (IBAction)playClicked:(UIButton *)sender {
    
    [IGAvPlayerManager shareManeger].delegate = self;
    if (![self.playpath isEqualToString:[IGAvPlayerManager shareManeger].currentURL]) {
        [[IGAvPlayerManager shareManeger] playMusic:self.playpath withPlayType:PlayTry];
        
    }else{
        if (![IGAvPlayerManager shareManeger].isPlay) {
            [[IGAvPlayerManager shareManeger] play];
        }else {
            [[IGAvPlayerManager shareManeger] pause];
        }
    }

}

- (void)dealloc {
    [self removeObserverBlocks];
}
#pragma mark -- IGAvPlayerManagerDelegate

- (void)updateProgressWith:(CGFloat)value {
    
    if ([IGAvPlayerManager shareManeger].isPlay) {
        [self.playBtn setProgress:value];
    }
}


- (void)PlayerChangePlayStateWith:(NSNotification *)notif
{
    NSInteger state = [notif.userInfo[kIGAvPlayerManagerState] integerValue];
    NSString  * url = notif.userInfo[kCurrentPlayURL];
    
    NSNumber * type = notif.userInfo[kPlayType];
    if (![url isEqualToString:self.playpath] || type.integerValue != PlayTry) {
        if (self.playBtn.selected) {
            self.playBtn.selected = NO;
            self.playBtn.progress = 0.f;
        }
        
        return;
    }
    switch (state) {
        case AVPlayerPlayStatePreparing:
        case AVPlayerPlayStateBeigin:
        case AVPlayerPlayStatePlaying:
        {
            self.playBtn.selected = YES;
            
            [self.playBtn  startSpinProgressBackgroundLayer];
        }
            break;
        case AVPlayerPlayStatePause:
        case AVPlayerPlayStateNotPlay:
        case AVPlayerPlayStateNotKnow:
        {
            self.playBtn.selected = NO;
            self.playBtn.progress = 0.f;
        }
            break;
        case AVPlayerPlayStateEnd:
        {
            self.playBtn.selected = NO;
            self.playBtn.progress = 0.f;
        }
            break;
        default:
        {
        }
            break;
    }
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
